<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>数组高级--元素添加</title>
    <script>
      let nums = [1, 2, 3]

      /* 数组元素增加(头部,尾部,中间)*/
      //头部元素添加
      //版本一: 对原数组直接修改
      /* for (let i = nums.length - 1; i >= 0; i--) {
        nums[i + 1] = nums[i]
      }
      nums[0] = 888 */

      //版本二: 原数组不变,新建一个数组来添加元素
      /* let newNums = []
      let idx = 1
      for (let i = 0; i < nums.length; i++) {
        newNums[idx++] = nums[i]
      }
      newNums[0] = 888
      console.log(newNums) */

      //尾部元素添加
      //nums[nums.length] = 999

      //中间添加元素
      //假设添加的是2号位置
      let pos = 2
      for (let i = nums.length - 1; i >= pos; i--) {
        nums[i + 1] = nums[i]
      }
      nums[pos] = 777
      console.log(nums)
    </script>
  </head>
  <body></body>
</html>
